<template>
    <div>
        <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="部门" prop="DeptID">
                <el-select v-model="ruleForm.DeptID" placeholder="请选择部门">
                    <el-option v-for="(item, index) in department" :key="index" 
                    :label="item.DName" :value="item.DId">{{ item.DName }}</el-option>
                </el-select>
            </el-form-item>
           
            <el-form-item label="职位名" prop="RoleName">
                <el-input v-model="ruleForm.RoleName"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="Type">
                <el-switch v-model="this.bool"></el-switch>
            </el-form-item>
            
            <el-form-item label="描述" prop="Remark">
                <el-input v-model="ruleForm.Remark"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" required>
                <el-form-item prop="LastTime" style="width: 200px;">
                    <el-date-picker type="date" 
                    placeholder="选择创建时间" 
                    v-model="ruleForm.LastTime"
                    style="width: 100%;"></el-date-picker>
                </el-form-item>
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>

export default {
    name: '',
    data() {
        return {
            ruleForm: {
                RoleID: 0,
                DeptID:2,
                RoleName: '清洁工',
                DeptName:'',
                Remark: 'xxx',
                Type:true,
                LastTime: new Date()
            },
            bool: true,
            department:[],
        }
    },
    props:{
        dialogVisible:Boolean
    },
    methods: {

        //提交表单
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //axios 提交
                    //this.ruleForm
                    console.log('this.ruleForm: ', this.ruleForm);

                    if(this.bool){
                        this.ruleForm.Type = 1;
                    }else{
                        this.ruleForm.Type = 0;
                    }

                    this.axios({
                        method:'post',
                        url:'Roles/Add',
                        params:{},
                        data:this.ruleForm
                    }).then(res=>{
                        console.log('res: ', res);
                        if(res.data.code>0){
                            this.$message({
                                message:res.data.msg,
                                type:'success'
                            });
                            ///* 改变父窗口的值来关闭弹窗 */
                            this.$emit('update:dialogVisible',false);
                            // 触发刷新
                            this.$emit('refresh') 

                        }else{
                            this.$message({
                                message:res.data.msg,
                                type:'error'
                            })
                        }
                    })
                    .catch(err=>{
                        console.log('err: ', err);

                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        //重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    },
    created(){
        this.axios({
            method:'get',
            url:'Roles/Dept'
        }).then(res=>{
            console.log('res: ', res);
            this.department = res.data.data;
        })
    }
}
</script>

<style scoped></style>